package ecmnext.gclient.gui;

/***************************************************************/
/*                     SOFTWARE SECTION                        */
/***************************************************************/
/*
 * <p>Name: ECMNext</p>
 * <p>Description: Simulation of the platform of trading Software</p>
 * <p>Licence: General Public License</p>
 * <p>Organisation: EC Marseille</p>
 * <p>Persons involved in the project : groupE Plateforme de Trading</p>
 * <p>Current version: 1.0</p>
 */


/***************************************************************/
/*                       IMPORT SECTION                        */
/***************************************************************/

import java.awt.*;
import javax.swing.*;

import ecmnext.utils.*;

/***************************************************************/
/*             NewMessageTimer CLASS SECTION                   */
/***************************************************************/
/**
 * <p>Title : NewMessageTimer</p>
 * <p>Description : </p>
 * @author Xuewen YANG ET Antoine COUDERC
 * @version 1.0
 */

public class NewMessageTimer
    extends Thread {
  JTextArea jtextArea;
  JScrollPane jScrollPane;
  int previousTextAreaHeight;
  int windowHeight;
  int messageHeight;

  public NewMessageTimer(JTextArea jTextAreaCommunication,JScrollPane jScrollPaneCommunication,int jTextAreaCommunicationHeightBeforeUpdate,int heightOfWindow) {
    jtextArea = jTextAreaCommunication;
    jScrollPane = jScrollPaneCommunication;
    previousTextAreaHeight = jTextAreaCommunicationHeightBeforeUpdate;
    windowHeight = heightOfWindow;
  }
  /**
   *
   */
  public void run() {
    try {
        messageHeight = jtextArea.getHeight() - previousTextAreaHeight;
        if (windowHeight >= messageHeight) {
            // set the scroll bar in order that the players don't need to adjust it to get new information
            jScrollPane.getVerticalScrollBar().setValue(jScrollPane.getVerticalScrollBar().getMaximum());
        }
        else {
          jScrollPane.getVerticalScrollBar().setValue(jScrollPane.getVerticalScrollBar().getMaximum() - messageHeight + windowHeight);
        }

        jtextArea.setBackground(Color.RED);
        //create the effect of dashed color in the information box at the end of each period
        this.sleep(300);
        jtextArea.setBackground(UIManager.getColor("Button.background").brighter());
        this.sleep(300);
        jtextArea.setBackground(UIManager.getColor("Button.background").darker());
        this.sleep(300);
        jtextArea.setBackground(UIManager.getColor("Button.background").brighter());
        this.sleep(300);
        jtextArea.setBackground(UIManager.getColor("Button.background"));
    }
      catch (InterruptedException ex) {
        Utils.logger.warn("NewMessageTimer sleep interrupted. " + ex.toString());
        jtextArea.setBackground(UIManager.getColor("Button.background"));
      }

  }
}
